//
//  HZStackView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/25.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit


//枚举
enum HZStackViewBtnType:Int{
    
    case toolbar = 0
     case mentionbutton = 1
    case trendbutton = 2
    case emoticonbutton = 3
    case add_background = 4
}

class HZStackView: UIStackView {
    
    //定义闭包
    var didBtnclosure:((btnType:HZStackViewBtnType)->())?


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置所有子控件均等分
        distribution = .FillEqually
        
        //添加子控件
        setupUI()
        
        //接收通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changedImgToAmotion:", name: "amotion", object: nil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //接收通知方法使表情按钮的图标改变
    @objc private func changedImgToAmotion(noti:NSNotification) {
        
        //获取目标按钮
        let btn = viewWithTag((HZStackViewBtnType.emoticonbutton).rawValue) as! UIButton
        
        let isIpu = noti.object as! Bool
        
        if isIpu == false {
            
            // 设置不同状态的图片
            btn.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
            btn.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)

        }else{
            
            
            // 设置不同状态的图片
            btn.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
            btn.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            
            
        }
    }
    
    
   
    
    //添加子控件
    private func setupUI(){
        
        
        addChildButton("compose_toolbar_picture", btnType: .toolbar)
        addChildButton("compose_mentionbutton_background", btnType: .mentionbutton)
        addChildButton("compose_trendbutton_background", btnType: .trendbutton)
        addChildButton("compose_emoticonbutton_background", btnType: .emoticonbutton)
        addChildButton("compose_add_background", btnType: .add_background)
        
    }
    
    
    
    private func addChildButton(imgNamed: String,btnType:HZStackViewBtnType) {
        let button = UIButton()
       
        //监听按钮点击
        button.addTarget(self, action: "didCilck:", forControlEvents: .TouchUpInside )
        
        //设置按钮的 tag 值
        //取得枚举的原始值
        button.tag = btnType.rawValue
      
        // 设置不同状态的图片
        button.setImage(UIImage(named: imgNamed), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgNamed)_highlighted"), forState: UIControlState.Highlighted)
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        addArrangedSubview(button)
    }

    
    //监听按钮点击方法
    @objc private func didCilck(btn:UIButton){
    
        //执行闭包
        didBtnclosure!(btnType: HZStackViewBtnType(rawValue: btn.tag)!)
    
    }
    
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
}
